package cool.taomu.toolkit.kademlia

import com.google.auto.service.AutoService
import com.google.inject.AbstractModule
import com.google.inject.Key
import com.google.inject.name.Names
import cool.taomu.toolkit.TaomuConfigure
import cool.taomu.toolkit.inter.IFunction
import cool.taomu.toolkit.kademlia.KademliaSerivceImpl.KademliaServiceProvider
import cool.taomu.toolkit.kademlia.module.KademliaModule
import cool.taomu.toolkit.kademlia.service.KademliaSerivce
import cool.taomu.toolkit.kademlia.service.RoutingTable
import cool.taomu.toolkit.kademlia.thrift.KademliaService
import cool.taomu.toolkit.kademlia.thrift.Node
import cool.taomu.toolkit.kademlia.utils.ResourceCollector
import cool.taomu.toolkit.platform.inter.AbsPlatform
import cool.taomu.toolkit.platform.inter.IPlatform
import cool.taomu.toolkit.rpc.thrift.module.ThritfTHsHaClientModule
import cool.taomu.toolkit.rpc.thrift.service.TaomuThriftTHsHaClient
import cool.taomu.toolkit.rpc.thrift.service.TaomuThriftTHsHaServer
import java.io.File
import org.apache.thrift.TProcessor
import org.apache.thrift.protocol.TProtocol
import org.slf4j.LoggerFactory

@Deprecated
@AutoService(IPlatform)
class KademliaPlugin extends AbsPlatform {

	val static LOG = LoggerFactory.getLogger(KademliaPlugin);

	override note() {
		"Kademlia DHT 服务"
	}

	override packages() {
		"cool.taomu.toolkit.kademlia.controller"
	}

	override module(TaomuConfigure configure) {
		new AbstractModule() {
			override configure() {
				LOG.info("加载 Kademlia 配置")
				install(new KademliaModule())
				bind(KademliaSerivce)
				// install(new ThritfTHsHaServerModule())
				install(new ThritfTHsHaClientModule())
				// TaomuGuice.setInjectorInstance(binder(), "kademlia.resource.injector")
				bind(TProcessor).annotatedWith(Names.named("kademlia.processor")).toProvider(KademliaServiceProvider)
				bind(ResourceCollector).asEagerSingleton
			}
		}
	}

	override start() {
		val injecotr = this.context.injectorSupplier.get
		val server = injecotr.getInstance(TaomuThriftTHsHaServer);
		val name = injecotr.getInstance(Key.get(String, Names.named("taomu.kademlia.name")))
		val ser = server.get(name, injecotr.getInstance(Key.get(TProcessor, Names.named("kademlia.processor"))))
		this.context.executor.submit([ser.serve])
	}

	override init() {
		// 服务加载在这里实现
		val injector = this.context.injectorSupplier.get
		var a = injector.getInstance(ResourceCollector);
		a.init();
		var torrent = new File("./torrent")
		if (!torrent.exists) {
			torrent.mkdirs
		}
		this.context.executor.submit([
			// 加入节点
			var cmap = injector.getInstance(TaomuThriftTHsHaClient)
			val rtable = injector.getInstance(RoutingTable)
			cmap.get("peer", new IFunction<TProtocol>() {
				override apply(TProtocol input) {
					var client = new KademliaService.Client(input);
					var node = new Node;
					node.id = rtable.localNode.id;
					node.host = rtable.localNode.host;
					node.port = rtable.localNode.port;
					var res = client.findNodeAndNode(node.id, node)
					println(res)
				}
			})
		])
	}

}
